<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Area;
use App\Models\Scene;
use Illuminate\Http\Request;

class SceneController extends Controller
{
    public function sceneList($search = ""){
        if($search != ''){
            $scene = Scene::where('name', 'like', '%'.$search.'%')->paginate(6);
        }else{
            $scene = Scene::paginate(6);
        }
        return view('admin.scene',['items'=>$scene,'type'=>'list','searchWord'=>$search]);
    }
    public function sceneHandler($type,$id = ''){
        $allArea = Area::get();
        if ($type == 'add'){
            if ($id != ''){
                return redirect('/admin/scene/edit/'.$id);
            }
            return view('admin.scene',['type'=>$type,'id'=>'new','area_id'=>'','allArea'=>$allArea]);
        }else if($type == 'edit'){
            if ($id == ''){
                return redirect('/admin/scene/add');
            }
            $scene = Scene::findOrFail($id);
            return view('admin.scene',['type'=>$type,'id'=>$id,'name'=>$scene->name,'intro'=>$scene->intro,
                'cover'=>$scene->cover,'area_id'=>$scene->area_id,'price'=>$scene->price,'allArea'=>$allArea]);
        }else{
            abort('404');
        }
    }

    public function saveScene(Request $request){
        $id = $request['id'];
        $area_id = $request['area_id'];
        $cover = $request['cover'];
        $name = $request['name'];
        $intro = $request['intro'];
        $price = $request['price'];
        if ($id == 'new'){
            $scene = new Scene();
        }else{
            $scene = Scene::findOrFail($id);
        }
        $scene->name = $name;
        $scene->cover = $cover;
        $scene->intro = $intro;
        $scene->price = $price;
        $scene->area_id=$area_id;
        $scene->save();
        return response()->json(['res'=>true,'msg'=>'保存成功']);
    }

    public function deleteScene(Request $request){
        $id = $request['id'];
        Scene::find($id)->delete();
        return response()->json(['res'=>true,'msg'=>'删除成功']);
    }
}
